<?php

namespace App\Http\Controllers\Category;


use App\Http\Controllers\Controller;
use App\Common\Common as common;
use App\Model\Category as category;
use Illuminate\Pagination\AbstractPaginator as paginator;

class CategoryController extends Controller
{
//     获取分类列表
    public function getList(){
        $request = $this->param;
        
        $validatorRule = ['merchantId'];
        
        $flag = common::validateRequestField($request,$validatorRule);
        if( $flag !== true ) common::error(0,$flag);
        
        $merchatId = $request['merchantId'];
        $where = ['merchantId'=>$merchatId];
        $page = $request['page'];
        $pageSize = 20;
        paginator::currentPageResolver(function() use ($page) {
            return $page;
        });
        $list = category::select(['parentId','name'])->where($where)->orderBy('sort','desc')->paginate($pageSize)->toArray();
        return common::success( $list );
    }
//    添加分类
    public function add(){
        $request = $this->param;
        
        $validatorRule = ['merchantId','name'];
        $flag = common::validateRequestField($request,$validatorRule);
        
        if( $flag !== true ) common::error(0,$flag);
        
        $insertData = [];
        $insertData['parentId'] = 0;
        $insertData['name'] = $request['name'];
        $insertData['merchantId'] = $request['merchantId'];
        $insertData['addTime'] = date('Y-m-d');
        
        if( isset($request['parentId']) && is_int($request['parentId']) ) $insertData['parentId'] = $request['parentId'];
        
        category::insertGetId($insertData);
        
        return common::success("添加分类成功");
    }
}
